<?php

namespace App\Member\Metrics\Examples;

use Dcat\Admin\Widgets\Metrics\Card;
use Illuminate\Http\Request;
use App\Models\WeekProfitReturn;
use Dcat\Admin\Admin;


class NewDevices extends Card
{
    protected $chartHeight = 100;
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();
        $this->height = 130;

        $this->user = Admin::user();
        $this->title('下级产品');
//        $this->dropdown([
//            '7' => 'Last 7 Days',
//            '28' => 'Last 28 Days',
//            '30' => 'Last Month',
//            '365' => 'Last Year',
//        ]);

        $this->weekProfitReturn = WeekProfitReturn::where('user_id', $this->user->id)
            ->select([
                \DB::raw('SUM(profit_count) as profit_count'),
                \DB::raw('SUM(yj_volume) as yj_volume'),
                \DB::raw('SUM(zt_profit) as zt_profit'),
                \DB::raw('SUM(zt_profit) as zt_profit'),
                \DB::raw('SUM(tj_profit) as yj_profit'),
                \DB::raw('SUM(tj_profit) as tj_profit'),
                \DB::raw('DATE_FORMAT(created_at,"%Y-%m") as date'),
            ])->groupBy('date')->get();


    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $generator = function ($len, $min = 10, $max = 300) {
            for ($i = 0; $i <= $len; $i++) {
                yield mt_rand($min, $max);
            }
        };

        switch ($request->get('option')) {
            case '28':
                // 卡片内容
                $this->withContent(mt_rand(400, 1000).'k');
                // 图表数据
                $this->withChart(collect($generator(28))->toArray());
                break;
            case '7':
            default:
                // 卡片内容
                $this->withContent(getNumberFormat($this->user->agent_balance));

        }
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
            'xaxis' => [
                'categories' => $this->weekProfitReturn->pluck('date')->toArray()
            ],
            'tooltip' => [
                'x' => ['show' => true],
            ]
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
            <!--<style>-->
                <!--#app .row .col-md-4 div{-->
                    <!--background-image: url("image/3.png");-->
                    <!--background-repeat:no-repeat;-->
                    <!--background-size:100% 100%;-->
                    <!-- -moz-background-size:100% 100%;-->
                <!--}-->
                <!--#app .row .col-md-4 h4 {color: #ffffff;}-->
                <!--#app .row .col-md-4 h1 {color: #ffffff;}-->
                <!---->
                <!--#app .row .col-md-4 .justify-content-between div{-->
                    <!--background-image: url("xxx.jpg")!important;-->
                <!--}-->
                <!--#app .row .col-md-4 .justify-content-between div h4{-->
                    <!--background-image: url("xxx.jpg")!important;-->
                <!--}-->
                <!--#app .row .col-md-4 .justify-content-between div h1{-->
                    <!--background-image: url("xxx.jpg")!important;-->
                <!--}-->
            <!--</style>-->

<div class="d-flex justify-content-between align-items-center mt-1">
    <div class="text-left">
        <h1 class="ml-1 font-lg-1">{$content}$</h1>
    </div>
</div>
HTML
        );
    }
}
